<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="cachelist" border style="width: 100%">
        <el-table-column prop="username" label="用户名" width="300%" align="center">
        </el-table-column>
        <el-table-column prop="user_id" label="微信号" align="center">
        </el-table-column>
        <el-table-column prop="name" label="操作" width="400%" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[3, 4, 5, 6]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改档口对话框 -->
    <el-dialog title="修改档口" :visible.sync="editdialogVisible" width="50%" @close="editdialogclose">
      <el-form :model="editFrom" :rules="editFromrules" ref="editFromref" label-width="70px" >

        <el-row :gutter="30">
          <el-col :span="14" :offset="5">
            <el-form-item>
            </el-form-item>
          </el-col>
          <el-col :span="14" :offset="6">
            <el-form-item label="用户编号">
              <el-input v-model="editFrom.user_id" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="6">
            <el-form-item label="档口" prop="stall">
              <el-select v-model="addbusiness.stall_id" placeholder="请选择">
                <el-option v-for="item in stalllist" :key="item.s_id" :label="item.s_name" :value="item.s_id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14" :offset="5">
            <el-form-item label="">
            <el-switch style="display: block" v-model="addbusiness.user_lok" active-text="商家" inactive-text="用户"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="14" :offset="5">
            <el-form-item>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="6" :offset="7">
          <el-button @click="editdialogVisible = false">取 消</el-button>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-button type="primary" @click="addbusinesse(addbusiness,editFrom)">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query:"",
        pagenum: 1,
        pagesize: 4,
      },
      total: 2,
      editdialogVisible: false,
      userlist:[],
      cachelist:[],
      editFromrules: {
/*         stall: [
            { required: true, message: '请选择档口归属', trigger: 'change' }
        ] */
      },
      editFrom: {},
      stalllist:[],
      addbusiness:{
        user_id:"",
        stall_id:"",
        user_lok:false
      }
    };
  },
  //界面创建时的调用函数
  created(){
    this.getuserlist()
  },
  methods: {
    //获取档口列表
    getuserlist(){
      this.$http.get('http://localhost:8090/user/findAll').then(response => {
            this.userlist=response.data
            console.log(this.userlist)
            this.total=this.userlist.length
            console.log(this.total)
            this.pagingdisplay(this.total,this.queryInfo.pagenum,this.queryInfo.pagesize)
        }, response => {
            console.log("error");
        });
      this.$http.get('http://localhost:8090/stall/findStallsName').then(response1 => {
        this.stalllist=response1.data
        }, response => {
          console.log("error");
        })
    },
    
    //分页显示函数
    pagingdisplay(total,pagenum,pagesize){
      this.cachelist=[]
      let p=(pagenum-1)*pagesize
      let o=pagenum*pagesize
      let j=0
      if(o<=this.total){
        for(let i=p;i<o;i++){
        this.cachelist[j]=this.userlist[i]
        j++
        }
      }
      else{
        for(let i=p;i<this.total;i++){
        this.cachelist[j]=this.userlist[i]
        j++
        }
      }
    },
    
    //编辑界面的显示数据导入
    handleEdit(data) {
        this.editdialogVisible = true;
        this.editFrom = data
    },
    
    //每页显示条数
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.queryInfo.pagenum = 1
      this.pagingdisplay(this.total,this.queryInfo.pagenum,this.queryInfo.pagesize)
    },
    
    //当前页变动
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum;
      this.pagingdisplay(this.total,this.queryInfo.pagenum,this.queryInfo.pagesize)
    },

    //修改对话框关闭事件
    editdialogclose(){
      this.$refs.editFromref.resetFields()
    },
    
    //修改档口
    addbusinesse(data1,data2){
      this.$refs.editFromref.validate((valid) => {
        if (!valid) return;
        //发起编辑请求
        if(data1.user_lok){
          console.log("到这里了")
          console.log(data2.user_id)
        let string = 'stall_id='+data1.stall_id+'&boss_id='+data2.user_id
        console.log(string)
        this.$http.post('http://localhost:8090/business/insertBoss?'+string).then(response => {
        console.log(response.data)
        }, response => {
            console.log("error");
        });
        console.log(data1)
        this.$message.success("商家绑定档口成功！");
        this.editdialogVisible = false;
        }
        else return;
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>